Ext.define('StaffServer.controller.RetireStaffController', {
	extend : 'Ext.app.Controller',

	models: ['RetireStaffModel'],
	stores: ['RetireStaffStore'],
	views:  ['retireStaff.RetireStaffGrid', 'retireStaff.RetireStaffMngr', 'retireStaff.RetireStaffSearchPanel', 'retireStaff.RetireStaffForm', 'retireStaff.RetireStaffWindow', 'retireStaff.RetireStaffExcelForm', 'retireStaff.RetireStaffExcelWindow'],
	
	tabView: 'StaffServer.view.retireStaff.RetireStaffMngr',
	
	retireStaffWindow:null,
	retireStaffForm:null,
	retireStaffExcelWindow:null,
	retireStaffExcelForm:null,
	submitFlag: null,
	retireId: null,
	
	refs : [ {
		ref : 'retireStaffSearchPanel',
		selector : 'retireStaffSearchPanel'
	}, {
		ref : 'retireStaffGrid',
		selector : 'retireStaffGrid'
	}],

	// 初始化执行语句
	init : function() {
		retireStaffWindow = Ext.widget("retireStaffWindow");
		retireStaffForm = Ext.ComponentQuery.query('retireStaffForm')[0];
		retireStaffExcelWindow = Ext.widget("retireStaffExcelWindow");
		retireStaffExcelForm = Ext.ComponentQuery.query('retireStaffExcelForm')[0];
		
		// 控制事件
		this.control({
			'retireStaffSearchPanel button[action=search]' : {
				click : this.search
			},
			'retireStaffSearchPanel button[action=reset]' : {
				click : this.reset
			},
			'retireStaffGrid button[action=saveRetireStaff]' : {
				click : this.saveRetireStaff
			},
			'retireStaffGrid button[action=updateRetireStaff]' : {
				click : this.updateRetireStaff
			},
			'retireStaffGrid button[action=deleteRetireStaff]' : {
				click : this.deleteRetireStaff
			},
			'retireStaffGrid button[action=importExcel]' : {
				click : this.importExcel
			},
			'retireStaffGrid button[action=exportExcel]' : {
				click : this.exportExcel
			},
			'retireStaffWindow button[action=submit]' : {
				click : this.submitWindow
			},
			'retireStaffWindow button[action=close]' : {
				click : this.closeWindow
			},
			'retireStaffExcelWindow button[action=submit]' : {
				click : this.submitExcelWindow
			},
			'retireStaffExcelWindow button[action=close]' : {
				click : this.closeExcelWindow
			},
			'retireStaffGrid': {
				itemdblclick: this.updateRetireStaff
			},
		});
	},

	//searchPanel查询
	search : function() {
		var searchPanel = this.getRetireStaffSearchPanel();
		var grid = this.getRetireStaffGrid();
		grid.store.on('beforeload', function(store){
			var searchParams = searchPanel.getForm().getFieldValues();
			Ext.apply(store.proxy.extraParams, searchParams);
        });
		Ext.ComponentQuery.query('retireStaffGrid > pagingtoolbar')[0].moveFirst();
	},
	
	//searchPanel重置
	reset: function(){
		var searchPanel = this.getRetireStaffSearchPanel();
		searchPanel.getForm().reset();
	},
	
	//新增
	saveRetireStaff: function(){
		submitFlag = "save";
		retireStaffWindow.setTitle("新增退休人员信息");
		retireStaffWindow.show();
		Ext.ComponentQuery.query('retireStaffForm field[name="retireId"]')[0].setDisabled(false);
	},
	
	//更新
	updateRetireStaff: function(){
		var record = this.getRetireStaffGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.ComponentQuery.query('retireStaffForm field[name="retireId"]')[0].setDisabled(true);
        retireStaffForm.loadRecord(record);
        retireId = record.data.retireId;
        submitFlag = "update";
        retireStaffWindow.setTitle("修改退休人员信息");
        retireStaffWindow.show();
	},
	
	
	//删除
	deleteRetireStaff: function(){
		var $this = this;
		var record = this.getRetireStaffGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.MessageBox.confirm('请确认', '确认删除吗?', function(btn, text){
            if (btn == 'yes') {
                showWaitMsg(); 
                Ext.Ajax.request({
                    url: 'retireStaff/deleteRetireStaff',
                    params: {
                    	retireId: record.get("retireId")
                    },
                    success: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        if (resp.success) {
                            Ext.Msg.alert('提示', '操作成功', function(){
                            	$this.search();
                            });
                        }
                        else {
                            Ext.Msg.alert('提示', resp.error);
                        }
                    },
                    failure: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
                    }
                });
            }
        });
	},
	
	//提交表单
	submitWindow: function(){
		if (!retireStaffForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = retireStaffForm.getValues();
		retireStaffForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: submitFlag == 'save' ? 'retireStaff/saveRetireStaff' : 'retireStaff/updateRetireStaff',
		    params: {
		    	retireId : retireId,
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//关闭窗口
	closeWindow: function(){
		retireStaffForm.getForm().reset();
		retireStaffWindow.hide();
	},
	
	
	//导入
	importExcel: function(){
		retireStaffExcelWindow.setTitle("导入文件");
		retireStaffExcelWindow.show();
	},
	
	
	//提交表单
	submitExcelWindow: function(){
		if (!retireStaffExcelForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = retireStaffExcelForm.getValues();
		retireStaffExcelForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: 'retireStaff/importExcel',
		    params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeExcelWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//关闭窗口
	closeExcelWindow: function(){
		retireStaffExcelForm.getForm().reset();
		retireStaffExcelWindow.hide();
	},
	
	
	exportExcel: function(){
		var searchPanel = this.getRetireStaffSearchPanel();
		var searchParams = searchPanel.getForm().getFieldValues();
		var jsonFilter = Ext.JSON.encode(searchParams);
		window.location.href = 'retireStaff/exportRetireStaffExcel?jsonFilter=' + jsonFilter;
	}
	
});